package com.aotain.oframework.util.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.aotain.oframework.constant.DateFormatConstant;
import com.aotain.oframework.util.lang.StringUtil;

/**
 * @author Jacob.cw.Zhang@gmail.com
 */
public class DateTimeUtil {

	public final static String FAT_COMMON = "dd/MM/yyyy";

	/**
	 * Get current Date
	 * 
	 * @return
	 */
	public static String getCurFormatDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.US);
		String curDate = sdf.format(new Date());
		return curDate;
	}

	/**
	 * Get Current DateTime
	 * 
	 * @return
	 */
	public static String getCurFormatDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		String curTime = sdf.format(new Date());
		return curTime;
	}

	/**
	 * Get Current DateTime
	 * 
	 * @return
	 */
	public static String getCurForDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
		String curTime = sdf.format(new Date());
		return curTime;
	}

	/**
	 * Get Current DateTime
	 * 
	 * @return
	 */
	public static String getCurForDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
		String curTime = sdf.format(new Date());
		return curTime;
	}

	/**
	 * add by xi'an ODC songying
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String curTime = sdf.format(new Date());
		return curTime;
	}

	// Month/Day/Year hour:minte:scend
	public static String transDataTime(Date datetime, String patter) {
		DateFormat format = new SimpleDateFormat(patter);
		return format.format(datetime);
	}

	public static boolean dateTimeIsLegal(Date fromDate, Date toDate) {
		return fromDate.before(toDate);
	}

	public static boolean dateTimeIsContainWeek(Date fromDate, Date toDate, String useFor) {
		boolean isContainWeek = false;
		if (dateTimeIsLegal(fromDate, toDate)) {
			int dateDiff = (int) ((toDate.getTime() - fromDate.getTime()) / (24 * 60 * 60 * 1000) + 1);
			if (dateDiff >= 7) {
				isContainWeek = true;
			} else {
				Calendar cal = Calendar.getInstance();
				cal.setTime(fromDate);
				int week = cal.get(Calendar.DAY_OF_WEEK);
				int[] weeks = new int[dateDiff];
				String[] strUserFor = useFor.split(",");
				for (int i = 0; i < dateDiff; i++) {
					if (i == 0) {
						weeks[i] = week + i;
					} else {
						weeks[i] = weeks[i - 1] + 1;
						if (weeks[i] == 8) {
							weeks[i] = 1;
						}
					}
				}
				int count = 0;
				for (String str : strUserFor) {
					for (int w : weeks) {
						if (str.equals(String.valueOf(w))) {
							count++;
						}
					}
				}
				if (count == strUserFor.length) {
					isContainWeek = true;
				}
			}
		}
		return isContainWeek;
	}

	public static Date getTomorrowDay(Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

	public static Date getDateByString(String dateTime, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		try {
			return df.parse(dateTime);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date getDateByString(String dateTime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return df.parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date getFirstDayOfMonth(Date date) {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(date);
		cDay1.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cDay1.getTime();
		return beginTime;
	}

	public static Date getLastDayOfMonth(Date date) {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(date);
		cDay1.set(Calendar.DAY_OF_MONTH, 1);
		cDay1.roll(Calendar.DAY_OF_MONTH, -1);
		Date endTime = cDay1.getTime();
		String eTime = new SimpleDateFormat("yyyy-MM-dd").format(endTime) + " 23:59:59";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(eTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getStrDateByNewFormat(String datetime, String format1, String format2) {
		String sDate = "";
		if (StringUtils.isNotEmpty(datetime)) {
			Date date = getDateByString(datetime, format1);
			sDate = transDataTime(date, format2);
		}
		return sDate;
	}

	/**
	 * Compare two dates with year, month and date but ignore time
	 * compareDateIgnoreTime(Date startDate, Date endDate)<br/>
	 * -1 : startDate > endDate<br/>
	 * 0 : startDate == endDate<br/>
	 * 1 : startDate < endDate<br/>
	 */
	public static int compareDateIgnoreTime(Date startDate, Date endDate) {
		Calendar calStartDate = Calendar.getInstance();
		Calendar calEndDate = Calendar.getInstance();
		calStartDate.setTime(startDate);
		calEndDate.setTime(endDate);
		int yearOfStartDate = calStartDate.get(Calendar.YEAR);
		int yearOfEndDate = calEndDate.get(Calendar.YEAR);
		int monthOfStartDate = calStartDate.get(Calendar.MONTH);
		int monthOfEndDate = calEndDate.get(Calendar.MONTH);
		int dateOfStartDate = calStartDate.get(Calendar.DATE);
		int dateOfEndDate = calEndDate.get(Calendar.DATE);

		if (yearOfStartDate > yearOfEndDate) {
			return -1;
		} else if (yearOfStartDate == yearOfEndDate) {
			if (monthOfStartDate > monthOfEndDate) {
				return -1;
			} else if (monthOfStartDate == monthOfEndDate) {
				if (dateOfStartDate > dateOfEndDate) {
					return -1;
				} else if (dateOfStartDate == dateOfEndDate) {
					return 0;
				} else {
					return 1;
				}
			} else {
				return 1;
			}
		} else {
			return 1;
		}
	}

	public static int compareDateIgnoreTime(String startDateStr, String endDateStr) {
		if (StringUtil.isEmptyString(startDateStr)) {
			return -2;
		}
		if (StringUtil.isEmptyString(endDateStr)) {
			return -2;
		}
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(startDateStr, DateFormatConstant.DATE_INPUT_MM);
			endDate = DateUtils.parseDate(endDateStr, DateFormatConstant.DATE_INPUT_MM);
			return compareDateIgnoreTime(startDate, endDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return -2;
		}
	}

	public static void main(String[] arg) {
		System.out.print(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateTimeUtil.getLastDayOfMonth(new Date())));
	}
}
